package com.mall.shopping.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.shopping.IProductService;
import com.mall.shopping.constant.GlobalConstants;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.PanelContentItemConverter;
import com.mall.shopping.converter.PanelConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Author: feisl
 * @Date: 2021/5/24 15:54
 */
@Service
@Slf4j
public class IProductServiceImpl implements IProductService {
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ItemDescMapper itemDescMapper;
    @Autowired
    ProductConverter productConverter;
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    PanelContentItemConverter panelContentItemConverter;



    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse productDetailResponse = new ProductDetailResponse();
        //查看商品信息
        ProductDetailDto productDetailDto = null;
        Long id = request.getId();
        try {
            productDetailDto = itemMapper.selectProductById(id);
        } catch (Exception e) {
            e.printStackTrace();
            productDetailResponse.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            productDetailResponse.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
            return productDetailResponse;
        }

        //设置detail字段，此处与接口文档保持一致，空字符串??
        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
        productDetailDto.setDetail(itemDesc.getItemDesc());
        //拆分图片信息，第一张图片，所有图片
        String[] images = productDetailDto.getProductImageBig().split(",");
        ArrayList<String> list = new ArrayList<>();
        //设置首张图片
        productDetailDto.setProductImageBig(images[0]);
        for (String image : images) {
            list.add(image);
        }
        //设置所有图片
        productDetailDto.setProductImageSmall(list);
        productDetailResponse.setProductDetailDto(productDetailDto);
        productDetailResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        productDetailResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return productDetailResponse;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        request.requestCheck();
        AllProductResponse allProductResponse = new AllProductResponse();
        allProductResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        allProductResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        PageHelper.startPage(request.getPage(), request.getSize());

        String column = "created";
        String dir = "desc";
        if ("1".equals(request.getSort())) {//升序
            column = "price";
            dir = "asc";
        } else if ("-1".equals(request.getSort())) {
            column = "price";
        }
        List<Item> items = null;
        PageInfo<Item> itemPageInfo = null;
        List<ProductDto> productDtos = null;
        try {
            items = itemMapper.selectItemFront(request.getCid(), column, dir, request.getPriceGt(), request.getPriceLte());
            itemPageInfo = new PageInfo<>(items);
            productDtos = productConverter.items2Dto(itemPageInfo.getList());
        } catch (Exception e) {
            log.error("ProductServiceImpl.getAllProduct Exception:" + e);
            ExceptionProcessorUtils.wrapperHandlerException(allProductResponse, e);
        }
        allProductResponse.setTotal(itemPageInfo.getTotal());
        allProductResponse.setProductDtoList(productDtos);
        return allProductResponse;
    }


    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse recommendResponse = new RecommendResponse();

        try {
            List<PanelContentItem> panelContentItems =
                    panelContentMapper.selectPanelContentAndProductWithPanelId(GlobalConstants.RECOMMEND_PANEL_ID);
            ArrayList<PanelContentItemDto> list = new ArrayList<>();
            for (int i = 0; i < panelContentItems.size(); i++) {
                PanelContentItemDto panelContentItemDto = panelContentItemConverter.item2ItemDto(panelContentItems.get(i));
                list.add(panelContentItemDto);
            }
            List<Panel> panels = panelMapper.selectPanelContentById(GlobalConstants.RECOMMEND_PANEL_ID);
            Set<PanelDto> panelDtos = PanelConverter.panel2Dto(panels, list);

            recommendResponse.setPanelContentItemDtos(panelDtos);
            recommendResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            recommendResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            recommendResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            recommendResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }
        return recommendResponse;
    }
}
